$(function() {
    var curPage = 1;
    var pageSize = 10;
    var discuccess3 = {
        getList: function() {
			$.ajax({
			    url: wsCommon.host + '/api/memberComments/hasdiscuss',
			    dataType: wsCommon.jsonType,
				data: {
                    pageSize: pageSize
                },
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var pager = res.data.pager;

							discuccess3.initPaging(pager.pages, pager.total, res);
						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
						}
					}
			    },
			    error: function() {
			        // console.log('接口返回失败');
			    }
			})
		},
        initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                discuccess3.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                discuccess3.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    $('#member_hasshow').html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>')
                }
        },
        getPageDates: function() {
            var self = this;

            $.ajax({
                url: wsCommon.host + '/api/memberComments/hasdiscuss',
                dataType: wsCommon.jsonType,
                data: {
					pageNum: curPage,
					pageSize: pageSize
				},
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
        randerList: function(pageData) {
            var tpl = '<colgroup>\
                            <col class="number-col">\
                            <col class="operate-col">\
                        </colgroup>\
                        <tbody>\
                           <tr class="tr-th">\
                                <th>商品信息</th>\
                                <th>评价状态</th>\
                           </tr>';

            $.each(pageData, function(index, item) {
                tpl += '<tr class="tr-bd">\
                            <td>\
                                <div class="goods-item">\
                                    <div class="p-img">\
                                        <a target="_blank" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'">\
                                            <img src="'+item.goodsPic+'" alt="">\
                                        </a>\
                                    </div>\
                                    <div class="p-msg">\
                                        <div class="p-name">\
                                            <a target="_blank" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'">'+item.goodsName+'</a>\
                                            <p style="color:#999;">购买时间：'+item.buyTimeStr+'</p>\
                                        </div>\
                                    </div>\
                                </div>\
                                <div class="goods-number">\
                                        x'+item.buyCount+'\
                                </div>\
                                <div class="clr"></div>\
                            </td>\
                            <td>\
                              <a href="./commented.html?commentId='+item.commentId+'" target="_blank" class="btn btn-simple"><span><span>查看评价</span></span></a>\
                            </td>\
                         </tr>';
            })
            tpl += '</tbody>';
            $('#member_hasshow table').html(tpl);
        },
        getTabNum: function() {
            $.ajax({
                url: wsCommon.host + '/api/memberComments/count',
                dataType: wsCommon.jsonType,
                success: function(res) {
                    if(res.code == 1) {
                        if(!res.result) {
                            wsCommon.setCookie('go_back_link', window.location.href);
                            window.location.href = './passport-login.html';
                            return;
                        }
                        var data = res.data;
                        if(data.count1 != 0) {
                            $('[data-role="tab1"]').append('<b class="sup">'+data.count1+'</b>');
                        }

                        if(data.count2 != 0) {
                            $('[data-role="tab2"]').append('<b class="sup">'+data.count2+'</b>');
                        }

                        if(data.count3 != 0) {
                            $('[data-role="tab3"]').append('<b class="sup">'+data.count3+'</b>');
                        }
                    } else {
                        iosOverlay({
                            text: res.reason,
                            duration: 1000
                        });
                    }
                },
                error: function(e) {
                }
            })
        },
        init: function() {
            this.getList();
            this.getTabNum();
        }
    }

    discuccess3.init();
})
